﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;

namespace ThereAreNoIrreplaceblePeople.Mailing
{
    public static class MailHelper
    {
        public static void ManageEmails(string senderEmail, List<string> receipentsEmails, string subjectName, string acceptLink, string rejectLink, DateTime date, int numberOfHours)
        {
            string emailsList = "<ul>";
            receipentsEmails.ForEach(re => emailsList += string.Format("<li>{0}</li>", re));
            emailsList += "</ul>";

            SendEmail(senderEmail,
                "There are no irreplecable people - notification",
                string.Format("I would like to inform you have successfully started looking for the replacement. We have sent information to the following lecturers <br />{0}", emailsList));

            foreach (string receipentEmail in receipentsEmails)//TODO add date after subject name
                SendEmail(receipentEmail,
                    "There are no irreplecable people - notification",
                    "You were selected as an replacament by " + senderEmail + " to conduct " + subjectName + " on " + date + " for " + numberOfHours + " hour(s). Are you interested?<br /> <a href=\"" + acceptLink + "\">Accept</a> or <a href=\"" + rejectLink + "\">Reject</a>");
        }

        public static void SendEmail(string receipentEmail, string subject, string contents)
        {
            try
            {
                // TODO: Smtp server needs to be installed and then the Web.config file needs to be changed 
                WebMail.SmtpServer = "smtp.poczta.onet.pl";
                WebMail.SmtpPort = 587;
                WebMail.UserName = "pbw.tanip@op.pl";
                WebMail.Password = "pbw123";

                WebMail.Send(
                        receipentEmail,
                        subject,
                        contents,
                        "pbw.tanip@op.pl"
                    );
            }
            catch (Exception ex)
            {
                throw new Exception("The problem occured while sending the email", ex);
            }
        }
    }
}